using Godot;
using System;
using Godot.Collections;

public partial class AbilityUpgradeCard : PanelContainer
{
	[Signal] 
	public delegate void SelectedEventHandler();

	private Label nameLable;
	private Label descriptionLable;

	bool disabled = false;
	

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		nameLable = GetNode<Label>("%NameLable");
		descriptionLable = GetNode<Label>("%DescriptionLable");

		GuiInput += OnGuiInput;
		MouseEntered += OnMouseEntered;
	}

	public void PlayIn(float delay = 0){
		Modulate = Colors.Transparent;		
		GetTree().CreateTimer(delay).Timeout += ()=>{
			GetNode<AnimationPlayer>("AnimationPlayer").Play("in");
		};
	}

	public void PlayDiscard(){
		GetNode<AnimationPlayer>("AnimationPlayer").Play("discard");
	}

	public void SetAbilityUpgrade(AbilityUpgrade upgrade){
		nameLable.Text = upgrade.name;
		descriptionLable.Text = upgrade.description;

	}

	public void SelectCard(){
		disabled = true;
		GetNode<AnimationPlayer>("AnimationPlayer").Play("selected");

		foreach(Node card in GetTree().GetNodesInGroup("upgrade_card")){
			if(card == this){
				continue;
			}
			(card as AbilityUpgradeCard).PlayDiscard();
		}

		GetNode<AnimationPlayer>("AnimationPlayer").AnimationFinished +=(animName)=>{
			GD.Print("动画",animName,"结束");
			if(animName == "selected"){
				this.EmitSignal(nameof(Selected));
			}
		};
	}

	public void OnGuiInput(InputEvent @event){
		if(disabled){
			return;
		}
		if( @event.IsActionPressed("left_click",false,false)){
			SelectCard();
		}
	}

	public void OnMouseEntered(){
		if(disabled){
			return;
		}
		GetNode<AnimationPlayer>("HoverAnimationPlayer").Play("hover");
	}
}
